package com.vsd.action;

import java.util.Collection;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.demo.annotation.LoginFilter;
import com.demo.bean.DataTable;
import com.demo.bean.Ret;
import com.demo.controller.CoreAdminController;
import com.demo.util.JSONUtils;
import com.demo.util.StringUtil;
import com.demo.validator.AdminLoginValidate;
import com.vsd.domain.City;
import com.vsd.service.CityService;

@Controller
@RequestMapping("city")
@LoginFilter(validateClz=AdminLoginValidate.class)
public class CityController extends CoreAdminController{
	@Autowired
	private CityService cityService;
 
	
	@RequestMapping("/list")
	@ResponseBody
	public String citylist(HttpSession session,@RequestParam("draw") Integer draw
			,@RequestParam("start") Integer start,@RequestParam("length") Integer length
			,@RequestParam("search[value]") String search,String areaCode,HttpServletRequest request){
		Page<City> list = cityService.findAll(areaCode, new PageRequest(start/length, length, new Sort(Direction.DESC, "sort")));
		return JSONUtils.serialize(new DataTable(draw+1,Integer.parseInt(list.getTotalElements()+""), list.getContent()));
	}
	
	
	@RequestMapping("/listByParent")
	@ResponseBody
	public String listByParent(String parent,HttpServletRequest request){
		if(StringUtils.isBlank(parent)) parent = "0";
		Collection<City> list = cityService.findByParent(parent, new Sort(Direction.DESC, "sort"));
		return JSONUtils.serialize(list);
	}
	
	@RequestMapping("/listByLevel")
	@ResponseBody
	public String listByLevel(HttpSession session,Integer level,String parent,HttpServletRequest request){
		Collection<City> list = cityService.findByLevel(level,parent);
		return JSONUtils.serialize(list);
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public String save(City city){
		if(StringUtils.isBlank(city.getId())){
			City c = cityService.findByCode(city.getParent());
			int level = 0;
			if(c!=null) level=c.getLevel() ;
			city.setId(StringUtil.getUUID());
			city.setCreateTime(new Date());
			city.setLevel(level+1);
			cityService.save(city);
		}else{
			city.setCode(null);
			city.setParent(null);
			cityService.updateByPrimaryKeySelective(city);
		}
		return JSONUtils.serialize(new Ret(0));
	}
	
	@RequestMapping("/findById")
	@ResponseBody
	public String findById(HttpSession session,String id){
		City city = cityService.findOne(id);
		return JSONUtils.serialize(city);
	}
	
	@RequestMapping("/unique")
	@ResponseBody
	public String unique(HttpSession session,String code,String parent){
		if(!"0".equals(parent)) code = parent+code;
		JSONObject ret = new JSONObject();
		City c = cityService.findByCode(code);
		if(c==null)  ret.put("valid", true);
		else ret.put("valid", false);
		return ret.toString();
	}
	
	
}
